package com.cqc.googleplay.view.part;

import com.cqc.googleplay.R;
import com.cqc.googleplay.application.MyApplication;
import com.cqc.googleplay.bean.AppInfoBean;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class AppInfoDesPart {

	private Context context;
	private View rootView;
	private LinearLayout ll_pat4;
	private TextView tv_desc;
	private TextView tv_author;
	private ImageView iv;
	private boolean flag = false;

	public AppInfoDesPart() {
		super();
		context = MyApplication.getContext();
	}

	public View getView(final ScrollView scrollview) {
		rootView = View.inflate(context, R.layout.part4_app_desc, null);
		ll_pat4 = (LinearLayout) rootView.findViewById(R.id.ll_pat4);
		tv_desc = (TextView) rootView.findViewById(R.id.tv_desc_part4);
		tv_author = (TextView) rootView.findViewById(R.id.tv_author_part4);
		iv = (ImageView) rootView.findViewById(R.id.iv_arrow_part4);

		ll_pat4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ValueAnimator valueAnimator = null;
				if (flag) {
					valueAnimator = ValueAnimator.ofInt(maxHeight,minHeight);
				} else {
					valueAnimator = ValueAnimator.ofInt(minHeight,maxHeight);
//					scrollview.scrollBy(0, 1000);//放这里无效
				}
				valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
					
					@Override
					public void onAnimationUpdate(ValueAnimator valueAnimator) {
						int value = (Integer) valueAnimator.getAnimatedValue();
						tv_desc.getLayoutParams().height = value;
						tv_desc.requestLayout();
						//放这里才有效
						if (!flag) {//未全部显示时
							scrollview.scrollBy(0, 1000);
						}
					}
				});
				valueAnimator.addListener(listener);
				valueAnimator.setDuration(350).start();
			}
		});
		return rootView;
	}

	private AnimatorListener listener = new AnimatorListener() {
		
		@Override
		public void onAnimationStart(Animator animator) {
			ViewPropertyAnimator.animate(iv).setDuration(350).rotationBy(180).start();
		}
		
		@Override
		public void onAnimationRepeat(Animator animator) {
			
		}
		
		@Override
		public void onAnimationEnd(Animator animator) {
			flag = !flag;
		}
		
		@Override
		public void onAnimationCancel(Animator animator) {
			
		}
	};
	private int minHeight;
	private int maxHeight;
	public void setData(AppInfoBean info) {
		tv_desc.setText(info.getDes());
		tv_author.setText(info.getAuthor());

		// 第一步：获取 5行和全部显示的高度
		tv_desc.setMaxLines(5);
		tv_desc.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				tv_desc.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				minHeight = tv_desc.getMeasuredHeight();

				tv_desc.setMaxLines(Integer.MAX_VALUE);//设置高度要放在getViewTreeObserver前面，否则怎么取值
				tv_desc.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						tv_desc.getViewTreeObserver().removeGlobalOnLayoutListener(this);
						maxHeight = tv_desc.getMeasuredHeight();
						
						tv_desc.getLayoutParams().height = minHeight;
						tv_desc.requestLayout();
					}
				});
			}
		});		
	}
}
